package com.zjjt.dc.modules.srv.controller.lw;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjjt.dc.common.api.CommonResult;
import com.zjjt.dc.common.query.QueryGenerator;
import com.zjjt.dc.modules.datacatch.dto.DcCatchDataLwPriceDTO;
import com.zjjt.dc.modules.datacatch.entity.lw.DcCatchDataLwPriceDr;
import com.zjjt.dc.modules.datacatch.entity.lw.DcCatchDataLwWh;
import com.zjjt.dc.modules.datacatch.service.lw.IDcDataCatchLwWhService;
import com.zjjt.dc.modules.datacatch.vo.lw.DcCatchDataLwPriceVO;
import com.zjjt.dc.modules.srv.service.lw.ILWPriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "辽文数据核对")
@Tag(name = "LWController", description = "辽文数据核对")
@RequestMapping("/liaowen")
public class LwPriceController {

    @Resource
    private ILWPriceService lwPriceService;
    @Resource
    private IDcDataCatchLwWhService dcDataCatchLwWhService;

    @ApiOperation("分页查询")
    @GetMapping("/pagelist")
    public CommonResult<IPage<DcCatchDataLwWh>> pageQuery(DcCatchDataLwWh entity, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest req) {

        QueryWrapper<DcCatchDataLwWh> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");

        if (req.getParameter("type") != null) {
            if (req.getParameter("type").equals("1")) {
                queryWrapper.isNull("price");
            } else if (req.getParameter("type").equals("2")) {
                queryWrapper.isNotNull("price");
            }
        }
        Page<DcCatchDataLwWh> page = new Page<DcCatchDataLwWh>(pageNo, pageSize);
        IPage<DcCatchDataLwWh> pageList = dcDataCatchLwWhService.page(page, queryWrapper);
        return CommonResult.success(pageList);
    }

    //导入execl
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public CommonResult<?> importExcel(@RequestParam("file") MultipartFile file) {
//        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
//        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
        // 获取上传文件对象
//            MultipartFile file = entity.getValue();
        ImportParams params = new ImportParams();
        params.setTitleRows(0);
        params.setHeadRows(1);
        params.setNeedSave(true);
        try {
            List<DcCatchDataLwPriceDr> list = ExcelImportUtil.importExcel(file.getInputStream(), DcCatchDataLwPriceDr.class, params);

            return CommonResult.success(lwPriceService.getDcCatchDataLwPriceVO(list));
        } catch (Exception e) {

        } finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return CommonResult.success();
    }

    //审核
    @RequestMapping(value = "/review", method = RequestMethod.POST)
    public CommonResult<?> review(@RequestBody DcCatchDataLwPriceVO dataLwPriceVO) {
        List<DcCatchDataLwPriceVO> list = new ArrayList<>();
        list.add(dataLwPriceVO);
        lwPriceService.review(list);
        List<DcCatchDataLwPriceVO> list1 = dataLwPriceVO.getList();
        List<DcCatchDataLwPriceVO> collect = list1.stream().filter(item -> !item.getId().equals(dataLwPriceVO.getId())).collect(Collectors.toList());

        return CommonResult.success(collect);
    }

    @RequestMapping(value = "/reviews", method = RequestMethod.POST)
    public CommonResult<?> reviews(@RequestBody DcCatchDataLwPriceDTO dataLwPriceVO) {
        List<Long> collect1 = dataLwPriceVO.getUpdateList().stream().map(item -> item.getId()).collect(Collectors.toList());
        lwPriceService.review(dataLwPriceVO.getUpdateList());
        List<DcCatchDataLwPriceVO> list1 = dataLwPriceVO.getList();
        List<DcCatchDataLwPriceVO> collect = list1.stream().filter(item -> !collect1.contains(item.getId())).collect(Collectors.toList());

        return CommonResult.success(collect);
    }

    @RequestMapping(value = "/download")
    public ModelAndView downloadAchievement(HttpServletRequest req, DcCatchDataLwWh entity) {
        QueryWrapper<DcCatchDataLwWh> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        if (req.getParameter("type") != null) {
            if (req.getParameter("type").equals("1")) {
                queryWrapper.isNull("price");
            } else if (req.getParameter("type").equals("2")) {
                queryWrapper.isNotNull("price");
            }
        }
        if (req.getParameter("multipleSelection") != null) {
            List<String> selectionList = Arrays.asList(req.getParameter("multipleSelection").split(","));
            queryWrapper.in("id", selectionList);
        }
        List<DcCatchDataLwWh> list = dcDataCatchLwWhService.list(queryWrapper);


        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, "业绩");
        mv.addObject(NormalExcelConstants.CLASS, DcCatchDataLwWh.class);
        ExportParams exportParams = new ExportParams();
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, list);
        return mv;
    }
}
